#include "BatchCheckInResponse.h"

BatchCheckInResponse::BatchCheckInResponse() {}

BatchCheckInResponse::BatchCheckInResponse(const QString &factoryNo, const QString &transId, const QDateTime &eventTime, const QString &resultCode, const QList<BatchCheckInResponsePcuInfo>& pcuInfo):
    factoryNo(factoryNo),
    transId(transId),
    eventTime(eventTime),
    resultCode(resultCode),
    pcuInfo(pcuInfo)
{

}

bool BatchCheckInResponse::fromJson(const QByteArray &jsonData)
{
    bool rtn = false;
    QJsonDocument doc = QJsonDocument::fromJson(jsonData);

    qDebug()<<QString::fromLocal8Bit(doc.toJson().toStdString());

    if(doc.isNull())
        rtn = false;
    else
    {
        QJsonObject json = doc.object();
        resultCode = json.value("resultCode").toString();
        success = json["resultCode"].toBool();

        qDebug()<<"resultCode = "<<resultCode;
        if(resultCode == "0")
        {
            factoryNo = json["factoryNo"].toString();
            transId = json["transId"].toString();
            eventTime = QDateTime::fromString(json["eventTime"].toString());

            const auto& pcuInfoArray = json["pcuInfo"].toArray();
            for(const auto& item:pcuInfoArray)
            {
                pcuInfo.append({
                                item.toObject().value("locationNo").toString(),
                                item.toObject().value("pcu").toString(),
                                item.toObject().value("resultMsg").toString()});
                resultMsg = item.toObject().value("resultMsg").toString();
            }
            rtn = true;

            jsonContent.fromStdString(doc.toJson().toStdString());
        }
        else
        {
            errorMsg = json["resultMsg"].toString();
            jsonContent = doc.toJson();
            qDebug()<<errorMsg;
            rtn = false;
        }
    }

    return rtn;
}

bool BatchCheckInResponse::isSuccess() const
{
    return success;
}

QString BatchCheckInResponse::getError() const
{
    return resultMsg;
}
